﻿<Window x:Class="DataGridExtensionsSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d"
        xmlns:src="clr-namespace:DataGridExtensionsSample"
        xmlns:dgx="urn:tom-englert.de/DataGridExtensions"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:componentModel="clr-namespace:System.ComponentModel;assembly=WindowsBase"
        DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}"
        Loaded="MainWindow_OnLoaded" TextOptions.TextFormattingMode="Display" UseLayoutRounding="True"
        Title="DataGrid Extensions Sample">
  <Window.Resources>

    <dgx:RegexContentFilterFactory x:Key="RegexContentFilterFactory" />
    <dgx:SimpleContentFilterFactory x:Key="SimpleContentFilterFactory" />

    <!-- Template for a simple integer dgx control. -->
    <ControlTemplate x:Key="IntegerFilter">
      <src:IntegerGreatherThanFilterControl Filter="{Binding Path=Filter, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=dgx:DataGridFilterColumnControl}}" />
    </ControlTemplate>

    <ControlTemplate x:Key="FilterWithPopup">
      <src:FilterWithPopupControl Filter="{Binding Path=Filter, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=dgx:DataGridFilterColumnControl}}"/>
    </ControlTemplate>
    
    <ControlTemplate x:Key="MultipleChoiceFilter">
      <src:MultipleChoiceFilter Filter="{Binding Path=Filter, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=dgx:DataGridFilterColumnControl}}"/>
    </ControlTemplate>

    <ControlTemplate x:Key="ComboboxFilter">
      <Grid d:DataContext="{d:DesignInstance dgx:DataGridFilterColumnControl}">
        <Grid>
          <Control Style="{DynamicResource {x:Static dgx:DataGridFilter.IconStyleKey}}"/>
          <ComboBox x:Name="ComboBox" Text="{Binding Path=Filter, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding SourceValues}" IsEditable="True" />
        </Grid>
      </Grid>
      <ControlTemplate.Triggers>
        <Trigger SourceName="ComboBox" Property="Text" Value="">
          <Setter TargetName="ComboBox" Property="Opacity" Value="0" />
        </Trigger>
        <Trigger SourceName="ComboBox" Property="Text" Value="{x:Null}">
          <Setter TargetName="ComboBox" Property="Opacity" Value="0" />
        </Trigger>
        <Trigger SourceName="ComboBox" Property="IsMouseOver" Value="True">
          <Setter TargetName="ComboBox" Property="Opacity" Value="1" />
        </Trigger>
        <Trigger SourceName="ComboBox" Property="IsFocused" Value="True">
          <Setter TargetName="ComboBox" Property="Opacity" Value="1" />
        </Trigger>
      </ControlTemplate.Triggers>
    </ControlTemplate>

    <Style TargetType="DataGrid">
      <Setter Property="HorizontalGridLinesBrush" Value="LightGray" />
      <Setter Property="VerticalGridLinesBrush" Value="LightGray" />
    </Style>
    
    <XmlDataProvider x:Key="ComboBoxSource" Source="SampleList.xml" XPath="Items/Item"/>
    
    <CollectionViewSource x:Key="SortedItems" Source="{Binding Items}">
      <CollectionViewSource.SortDescriptions>
        <componentModel:SortDescription PropertyName="Column1"/>
      </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>

    <Style x:Key="DataGridComboBoxStyle" TargetType="ComboBox">
      <Setter Property="BorderThickness" Value="0"/>
      <Setter Property="Padding" Value="3,2,0,0"/>
      <Setter Property="Foreground" Value="Green"/>
    </Style>
    <Style x:Key="DataGridTextBlockStyle" TargetType="TextBlock">
      <Setter Property="Foreground" Value="Blue"/>
    </Style>

  </Window.Resources>

  <Grid>
    <Button x:Name="ReloadButton" Content="Reload data" VerticalAlignment="Top" HorizontalAlignment="Right" Click="Reload_Click" />

    <TabControl>

      <TabItem Header="Basic usage">
        <DockPanel>
          <StackPanel Margin="10" DockPanel.Dock="Top">
            <TextBlock TextWrapping="WrapWithOverflow">
              <Run>This tab shows the very basic usage.</Run><LineBreak />
              <Run>Just set the IsAutoFilterEnabled property to True, and get the simple but efficient default filter.</Run>
            </TextBlock>
            <Decorator Height="10" />
            <StackPanel Orientation="Horizontal">
              <CheckBox x:Name="IsFilterEnabled" DockPanel.Dock="Top" IsChecked="True" Content="Enable filters" />
            </StackPanel>
          </StackPanel>
          <DataGrid Name="Grid1"
                    ItemsSource="{Binding Items}"
                    dgx:DataGridFilter.IsAutoFilterEnabled="{Binding Path=IsChecked, ElementName=IsFilterEnabled}"/>
        </DockPanel>
      </TabItem>

      <TabItem Header="Extended 1">
        <DockPanel>
          <TextBlock DockPanel.Dock="Top" Margin="10" TextWrapping="WrapWithOverflow">
            <Run>This tab shows how to customize the filters when using auto generated columns. See the DataGrid_AutoGeneratingColumn event handler for details.</Run><LineBreak/>
            <Run>- Overrides the content filter factory to use the regex matching instead of the default simple "string contains" matching.</Run><LineBreak/>
            <Run>- Uses the sample IntergerFilter for the Index column.</Run>
          </TextBlock>
          <DataGrid ItemsSource="{Binding Items}"
                    AutoGeneratingColumn="DataGrid_AutoGeneratingColumn"
                    dgx:DataGridFilter.ContentFilterFactory="{StaticResource RegexContentFilterFactory}"
                    dgx:DataGridFilter.IsAutoFilterEnabled="True">
            <DataGrid.Resources>
              <!-- Override the filter icon used in the default templates with a fat orange symbol; active for this grid only -->
              <ControlTemplate x:Key="{x:Static dgx:DataGridFilter.IconTemplateKey}">
                <Path Data="M0,0 L10,0 7,5 7,9 3,9 3,5 Z" Fill="Orange" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="2,0"/>
              </ControlTemplate>
            </DataGrid.Resources>

            <i:Interaction.Behaviors>
              <dgx:ExtendedStarSizeBehavior/>
              <dgx:BeginEditOnCtrlEnterBehavior/>
              <dgx:DisableTargetWhileEditingBehavior Target="{Binding ElementName=ReloadButton}"/>
            </i:Interaction.Behaviors>

          </DataGrid>
        </DockPanel>
      </TabItem>

      <TabItem Header="Extended 2">
        <DockPanel>
          <TextBlock DockPanel.Dock="Top" Margin="10" TextWrapping="WrapWithOverflow">
            <Run>This tab shows how to customize the filters in XAML when using explicit generated columns. Read the comments in the XAML for details.</Run><LineBreak/>
            <Run>Overrides the icon template to show an orange filter symbol.</Run>
          </TextBlock>
          <DataGrid ItemsSource="{Binding Source={StaticResource SortedItems}}" AutoGenerateColumns="False" FrozenColumnCount="2"
                    dgx:DataGridFilter.GlobalFilter="{Binding ExternalFilter}"
                    dgx:DataGridFilter.IsAutoFilterEnabled="True"
                    dgx:Tools.ApplyInitialSorting="True"
                    dgx:DataGridFilter.FilterEvaluationDelay="0:0:0">
            <i:Interaction.Behaviors>
              <dgx:ExtendedStarSizeBehavior/>
              <dgx:BeginEditOnCtrlEnterBehavior/>
            </i:Interaction.Behaviors>

            <!-- Override the default styles for some columns... -->
            <dgx:ColumnStyles.DefaultColumnStyles>
              <dgx:DataGridColumnStyleCollection>
                <dgx:DataGridColumnStyle ColumnType="DataGridComboBoxColumn" EditingElementStyle="{StaticResource DataGridComboBoxStyle}"/>
                <dgx:DataGridColumnStyle ColumnType="DataGridTextColumn" ElementStyle="{StaticResource DataGridTextBlockStyle}"/>
              </dgx:DataGridColumnStyleCollection>
            </dgx:ColumnStyles.DefaultColumnStyles>

            <DataGrid.Resources>
              <!-- Override default icon style to have a blue icon that changes to red when the column header is hovered. -->
              <Style TargetType="Control" x:Key="{x:Static dgx:DataGridFilter.IconStyleKey}" BasedOn="{StaticResource {x:Static dgx:DataGridFilter.IconStyleKey}}">
                <Setter Property="Foreground" Value="Blue" />
                <Style.Triggers>
                  <DataTrigger Binding="{Binding Path=IsMouseOver, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridColumnHeader}}" Value="True">
                    <Setter Property="Foreground" Value="Red" />
                  </DataTrigger>
                </Style.Triggers>
              </Style>

              <DataTemplate x:Key="CustomHeaderWithLeftAlignedSearch">
                <DockPanel>
                  <!-- The control to host the filter UI for this column -->
                  <dgx:DataGridFilterColumnControl DockPanel.Dock="Left" />
                  <ContentPresenter x:Name="PART_Content" Content="{Binding}"
                                    SnapsToDevicePixels="{Binding SnapsToDevicePixels, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridColumnHeader}}"
                                    HorizontalAlignment="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridColumnHeader}}"
                                    VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridColumnHeader}}" />
                </DockPanel>
              </DataTemplate>
              
            </DataGrid.Resources>

            <DataGrid.Columns>
              <!-- Use a check box column for the boolean property "Flag" but override the column data type to use the default text filter template; also
                  overrides the HeaderTemplate to show customization of the filter control. -->
              <DataGridCheckBoxColumn Header="Bool/Text"
                                      HeaderTemplate="{StaticResource ResourceKey=CustomHeaderWithLeftAlignedSearch}"
                                      Binding="{Binding Path=Flag, Mode=OneWay}"
                                      dgx:DataGridFilterColumn.Template="{StaticResource {x:Static dgx:DataGridFilter.TextColumnFilterTemplateKey}}" />
              <!-- Use the integer filter for the integer column. -->
              <DataGridTextColumn Header="Integer/Custom"
                                  Binding="{Binding Index, Mode=OneWay}"
                                  Width="*"
                                  dgx:DataGridFilterColumn.Template="{StaticResource IntegerFilter}"
                                  />
              <!-- Hide the filter for this column. -->
              <DataGridTextColumn Header="Text/No Filter" Binding="{Binding Column1}" Width="*"
                                  dgx:DataGridFilterColumn.IsFilterVisible="False"
                                  dgx:Tools.IsMultilineEditingEnabled="True"
                                  SortDirection="Descending" />
              <!-- Use the default filter for this column. -->
              <DataGridTextColumn Header="Text/Text" Binding="{Binding Column2, Mode=OneWay}" Width="*"/>
              <!-- Use a simple combo box for filtering -->
              <DataGridComboBoxColumn Header="Text/Custom" SelectedValueBinding="{Binding Column5}" 
                                      Width="*" dgx:DataGridFilterColumn.Template="{StaticResource MultipleChoiceFilter}" 
                                      ItemsSource="{Binding Source={StaticResource ComboBoxSource}}"/>
              <!-- Use a simple combo box for filtering, but override the editing element style -->
              <DataGridComboBoxColumn Header="Text/Custom" SelectedValueBinding="{Binding Column5}" 
                                      Width="*" dgx:DataGridFilterColumn.Template="{StaticResource ComboboxFilter}" 
                                      ItemsSource="{Binding Source={StaticResource ComboBoxSource}}">
                <DataGridComboBoxColumn.ElementStyle>
                  <Style TargetType="ComboBox">
                    <Setter Property="ItemTemplate">
                      <Setter.Value>
                        <DataTemplate>
                          <TextBlock Text="{Binding InnerText}" Background="BlueViolet"/>
                        </DataTemplate>
                      </Setter.Value>
                    </Setter>
                  </Style>
                </DataGridComboBoxColumn.ElementStyle>
              </DataGridComboBoxColumn>
              <!-- Use a special filter using a toggle button with a popup to enter more than one condition. -->
              <DataGridTextColumn Header="Double/Custom" Binding="{Binding Probability, Mode=OneWay}" Width="*"
                                  dgx:DataGridFilterColumn.Template="{StaticResource FilterWithPopup}" />
              <!-- A template column may contain everything, so we need to specify the SortMemberPath explicitly...-->
              <DataGridTemplateColumn Header="Template" SortMemberPath="Probability">
                <DataGridTemplateColumn.CellTemplate>
                  <DataTemplate>
                    <TextBlock Margin="2" Text="{Binding Probability}"/>
                  </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
              </DataGridTemplateColumn>
            </DataGrid.Columns>
          </DataGrid>
        </DockPanel>
      </TabItem>

      <TabItem Header="Copy/Paste">
        <DataGrid x:Name="CopyPasteDataGrid" SelectionUnit="Cell" ItemsSource="{Binding Items}">
          <DataGrid.ContextMenu>
            <ContextMenu>
              <MenuItem Header="Copy" Click="Copy_Click"/>
              <MenuItem Header="Paste" Click="Paste_Click"/>
            </ContextMenu>
          </DataGrid.ContextMenu>
        </DataGrid>
      </TabItem>

    </TabControl>

  </Grid>
</Window>